package com.powerhua.cms.web.servlet.action;

import com.powerhua.core.utils.CommonUtils;
import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.powerhua.cms.CmsUtils;
import com.powerhua.cms.domain.Content;
import com.powerhua.cms.domain.logic.ContentManager;
import com.powerhua.cms.persistence.file.CmsFileManager;
import com.powerhua.fms.FmsUtils;
import com.powerhua.fms.logic.FileManager;
import com.powerhua.privilege.PrivilegeUtils;
import com.powerhua.privilege.domain.User;

public class ContentImpl implements ActionInterface {

	/**
	 * 
	 */
	public String doAction(HttpServletRequest req) {
		String resultStr = "";

		User user = null;
		HttpSession session = req.getSession(true);
		Object obj = session.getAttribute(PrivilegeUtils.SESSION_CURRENT_USER);
		if (obj != null) {
			user = (User) obj;
		} else {
			resultStr = "/permission/login.jsp";
			return resultStr;
		}

		String action = (String) req.getParameter("action");
		if ("editContent".equalsIgnoreCase(action)) {
			String contentId = req.getParameter("contentId");
			resultStr = editContent(contentId, req);
		} else if ("getListPage".equalsIgnoreCase(action)) {
			resultStr = getListPage(req);
		} else if ("getContentsByCategory".equalsIgnoreCase(action)) {
			String categoryId = req.getParameter("categoryId");
			resultStr = getContentsByCategoryId(categoryId, user.getUserId(), req);
		} else {
			resultStr = getContentsByUserId(user.getUserId(), req);
		}

		return resultStr;
	}
	
	/**
	 * 获得文档列表页面
	 * 
	 * @param req
	 * @return
	 */
	private String getListPage(HttpServletRequest req) {
		req.setAttribute("type", "list");
		req.setAttribute("forward", new Boolean(true));
		return "/docs/list.jsp";
	}

	/**
	 * 通过用户名获取文档列表
	 * 
	 * @param userId
	 * @param req
	 * @return
	 */
	private String getContentsByUserId(String userId, HttpServletRequest req) {
		ContentManager cm = CmsUtils.getContentManager();
		List list = cm.getContentsByUserId(userId);
		req.setAttribute("contentList", list);
		req.setAttribute("forward", new Boolean(true));
		return "/docs/hide.jsp";
	}

	/**
	 * 通过类型获取文档列表
	 * 
	 * @param categoryId
	 * @param userId
	 * @param req
	 * @return
	 */
	private String getContentsByCategoryId(String categoryId, String userId,
			HttpServletRequest req) {
		ContentManager cm = CmsUtils.getContentManager();
		List list = cm.getContentByCategoryId(categoryId, userId);
		req.setAttribute("contentList", list);
		req.setAttribute("forward", new Boolean(true));
		return "/docs/hide.jsp";
	}
	
	/**
	 * 
	 * @param contentId
	 * @param req
	 * @return
	 */
	private String editContent(String contentId, HttpServletRequest req) {
		ContentManager cm = CmsUtils.getContentManager();
		Content c = new Content();
		if(contentId != null && !"".equalsIgnoreCase(contentId)) {
			// 修改
			c = cm.getContentById(contentId);
		}
		
		req.setAttribute("content", c);
		req.setAttribute("forward", new Boolean(true));
		return "/docs/edit.jsp";
	}
	
	/**
	 * 
	 * @param c
	 * @param req
	 * @return
	 */
	private String saveContent(Content c, HttpServletRequest req) {
		ContentManager cm = CmsUtils.getContentManager();
		CmsFileManager cfm = CmsUtils.getCmsFileManager();
		if(c.getContentId() != null && !"".equals(c.getContentId())) {
			cm.updateContent(c);
		} else {
			String contentId = CommonUtils.getUuid();
			File file = cfm.getContentFile(contentId);
			c.setContentId(contentId);
			FileManager fm = FmsUtils.getFileManager();
			fm.saveFile(file, c.getContentDetail());
			
			// 
			cm.insertContent(c);
		}
		
		req.setAttribute("redirect", new Boolean(false));
		return "";
	}
	
	private String deleteContents(String contentIds, HttpServletRequest req) {
		return "";
	}

}
